﻿using Microsoft.Extensions.Localization;
using Microsoft.WindowsAPICodePack.Dialogs;
using StudentManagement.IService;
using StudentManagement.Model.DTO;
using StudentManagement.Model.Entitys;
using StudentManagement.Model.Enums;
using StudentManagement.Tools;
using StudentManagement.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StudentManagement
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region 私有属性/构造函数及方法

        private readonly IStudentService _studentService;
        public MainWindow(IStudentService studentService)
        {
            InitializeComponent();

            _studentService = studentService;
        }

        private void Load()
        {
            var data = _studentService.Search(vm.QS, 10, 1);
            listView.ItemsSource = data.Item1;
        }

        private readonly StudentQuery vm = new StudentQuery();

        /// <summary>
        /// 双向绑定
        /// </summary>
        private void TwoWayBinding()
        {
            txtQuery.SetBinding(TextBox.TextProperty, new Binding(nameof(vm.QS)) { Source = vm });
        }

        #endregion



        private void winStudentManagement_Loaded(object sender, RoutedEventArgs e)
        {
            Load();
            TwoWayBinding();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var win = ServiceLocator.GetWindow<Views.Student.AddWindow>();
            win.LoadParentData = Load;
            win.ShowDialog(this);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItems.Count < 1)
            {
                "请选择一条数据".ShowInfoAlert();
                return;
            }

            var win = ServiceLocator.GetWindow<Views.Student.EditWindow>();
            win.LoadParentData = Load;
            win.EntityId = (listView.SelectedItems[0] as StudentQueryDTO).Id;
            win.ShowDialog(this);
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItems.Count != 1)
            {
                "请选择一条数据".ShowInfoAlert();
                return;
            }

            var entity = listView.SelectedItem as StudentQueryDTO;

            var confirmState = $"确认删除【{entity.Name}】么？".ShowConfirmAlert();

            if (confirmState != MessageBoxResult.OK) return;

            _studentService.Delete((listView.SelectedItem as StudentQueryDTO).Id);

            Load();
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            //dialog.InitialDirectory = "C:\\";
            dialog.IsFolderPicker = true;
            var res = dialog.ShowDialog();
            if (res == CommonFileDialogResult.Ok)
            {
                var fileName = $"{dialog.FileName}\\学生列表.xlsx";
                EPPlusHelp.SaveExcelByDataTable(fileName, _studentService.GetData(vm.QS));
                $"导出成功。文件地址：{fileName}".ShowInfoAlert();
            }
        }
    }
}
